package com.teaching.teachingsupport.service.impl.studentserviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.teaching.teachingsupport.common.pojo.dataobject.Notification;
import com.teaching.teachingsupport.common.pojo.dataobject.User;
import com.teaching.teachingsupport.common.utils.SecurityUtils;
import com.teaching.teachingsupport.mapper.studentviewmapper.NotificationMapper;
import com.teaching.teachingsupport.service.service2.studentservice.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class NotificationServiceImpl implements NotificationService {

    private final NotificationMapper notificationMapper;
    private final SecurityUtils securityUtils;

    @Override
    public List<Notification> getNotificationsByUsername(String username, String password) {
        // 验证用户名和密码
        User user = securityUtils.validateUser(username, password);
        if (user == null) {
            return List.of();
        }

        return getNotificationsByUserId(user.getUserId());
    }

    @Override
    public List<Notification> getNotificationsByUserId(Long userId) {
        return notificationMapper.selectList(
            new LambdaQueryWrapper<Notification>()
                .eq(Notification::getUserId, userId)
                .orderByDesc(Notification::getCreatedTime)
        );
    }
} 